<template>
	<view>
		<uni-swipe-action v-if="list.length>0">
			<uni-swipe-action-item v-for="(item,index) in list" :options="options" :key="index" @change="swipeChange" @click="swipeClick($event,index)">
				<media-list type="one" :item="item" :index="index"></media-list>
			</uni-swipe-action-item>
		</uni-swipe-action>
		<!-- 默认样式 -->
		<view v-else class="flex w-100 align-center justify-center text-secondary" style="height: 100rpx;">
			暂无数据
		</view>
		<view v-if="list.length>=10" class="flex align-center justify-center py-3 bg-light">
			<text class="text-muted font">{{loadtext}}</text>
		</view>
	</view>
</template>

<script>
	import mediaList from '@/components/common/media-list.vue';
	import uniSwipeAction from '@/components/uni-ui/uni-swipe-action/uni-swipe-action.vue'
	import uniSwipeActionItem from '@/components/uni-ui/uni-swipe-action-item/uni-swipe-action-item.vue'
	import {mapState} from 'vuex';
	export default {
		components: {
			mediaList,
			uniSwipeAction,
			uniSwipeActionItem
		},
		computed:{
			...mapState({
				user_id:state=>{
					return state.user?state.user.id:0
				}
			})
		},
		data() {
			return {
				options: [{
					text: '取消收藏',
					style: {
						backgroundColor: 'rgb(255,58,49)'
					}
				}],
				list:[],
				page:1,
				loadtext:'上拉加载更多'
			}
		},
		onLoad() {
			this.getData()
		},
		onPullDownRefresh() {
			this.page=1
			this.getData().then(res=>{
				uni.showToast({
					title:'刷新成功',
					icon:'none'
				})
				uni.stopPullDownRefresh()
			}).catch(err=>{
				uni.showToast({
					title:'刷新失败',
					icon:'none'
				})
				uni.stopPullDownRefresh()
			})
		},
		onReachBottom() {
			if(this.loadtext!=='上拉加载更多'){
				return
			}
			this.page+=1
			this.getData().catch(err=>{
				this.page-=1
				this.loadtext="上拉加载更多"
			})
		},
		methods: {
			getData(){
				this.loadtext='加载中...'
				return this.$H.get(`/fava_list/1?user_id=${this.user_id}`).then(res=>{
					this.list=this.page===1?res:[...this.list,...res],
					this.loadtext=this.page*10===this.list.length?'上拉加载更多':'没有更多啦'
				})
			},
			swipeChange(e) {
				console.log('返回：', e);
			},
			swipeClick(e, index) {
				console.log()
				this.$H.post('/fava/video', {
					video_id: this.list[index].id
				}, {
					token: true
				}).then(res => {
					uni.showToast({
						title: "取消收藏成功",
						icon: 'none'
					});
					this.getData()
				})
			}
		}
	}
</script>

<style>

</style>
